package 临时工具.shangyu2;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.digitenjoy.common.core.utils.GPSDataConvertUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import 临时工具.ExportExcel;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Data
public class pt implements Serializable {
    //读取文件
    static String filePathws = "C:\\Users\\瑾瑜\\Desktop\\sql\\front\\source\\gw\\wsgxpt.geojson";
    static String filePathys = "C:\\Users\\瑾瑜\\Desktop\\sql\\front\\source\\gw\\YSGXPT.geojson";
    static String outPath = "C:\\Users\\瑾瑜\\Desktop\\sql\\front\\source\\gw\\pt_out";
    public static void main(String[] args) throws IOException {

        Set<String> set = new HashSet<>();
        JSONArray list= new JSONArray();
        togx(filePathws,list,"WS",set);
        togx(filePathys,list,"YS",set);
        List<JSONArray> lists = splitList(list, 10000);
        for(int i = 0; i <lists.size(); i++){
            System.out.println("共"+lists.size()+"组,写入第"+(i+1)+"文件");
            ex(set, lists.get(i),outPath+(i+1));
        }
    }
    public static <T> List<JSONArray> splitList(JSONArray list, int size) {
        List<JSONArray> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i += size) {
            int end = Math.min(i + size, list.size());
            result.add(new JSONArray(list.subList(i, end)));
        }
        return result;
    }
    private static void togx(String filePath,JSONArray list,String type, Set<String> set ){
        String content = FileUtil.readUtf8String(filePath);
        JSONObject jsonObject = JSONObject.parseObject(content);
        JSONArray features = jsonObject.getJSONArray("features");

        for (int i = 0; i < features.size(); i++) {
            JSONObject feature = features.getJSONObject(i);
            JSONObject properties = feature.getJSONObject("properties");
            JSONArray coordinates = feature.getJSONObject("geometry").getJSONArray("coordinates");

            JSONObject object = new JSONObject();
            for (String key:properties.keySet()) {
                if ("objectid".equals(key.toLowerCase())){
                    continue;
                }
                object.put(key.toLowerCase(), properties.getString(key));
            }
            Double x = coordinates.getDouble(0);
            Double y = coordinates.getDouble(1);
            object.put("x84", x);
            object.put("y84", y);
            double[] doubles1 = GPSDataConvertUtil.transformWGS84ToGCJ02(x,y);
            object.put("x02", String.valueOf(doubles1[0]));
            object.put("y02", String.valueOf(doubles1[1]));

            object.put("type",type);


            String jssj = object.getString("jssj");
            if (StringUtils.isNotBlank(jssj)){
                jssj=jssj.replaceAll("T", " ");
            }else {
                jssj = "1899-12-30 00:00:00";
            }

            object.put("jssj", jssj);

            list.add(object);
            set.addAll(object.keySet());

        }

    }
    private static void ex(Set<String> set, JSONArray list ,String path) throws IOException {
        ExportExcel exportExcel = new ExportExcel();

        ArrayList<String> list1 = new ArrayList<String>() {{
            add("bzmc");
            add("gdxs");
            add("jdss");
            add("pcsj");
        }};
        List<String> arr = new ArrayList<>(set).stream().filter(s -> !list1.contains(s)).collect(Collectors.toList());
        for(int j = 0; j < arr.size(); j++){
            exportExcel.setValue(j, arr.get(j));
        }
        exportExcel.addRowIndex();
        for(int i = 0; i < list.size(); i++){
            JSONObject obj = list.getJSONObject(i);
            for(int j = 0; j < arr.size(); j++){
                exportExcel.setValue(j, obj.getString(arr.get(j)));
            }
            exportExcel.addRowIndex();
        }
        exportExcel.write(Files.newOutputStream(new File(path+".xlsx").toPath()));
    }
}
